package com.guiadigital;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.guiadigital.entidades.Punto;
import com.guiadigital.servicios.LugaresInformacion;

public class Ruta extends MapActivity {
private MapView map = null;
	
	private MapController mapController;
	private MyLocationOverlay myLocationOverlay;
	
	private MapView mapView;
	protected List<Overlay> mapOverlays;
	
		private void inicializeMap(){
	     mapView = (MapView) findViewById(R.id.mapview);
	        mapView.setBuiltInZoomControls(true);        
	        mapController = mapView.getController();
	        mapController.setZoom(9);  
	        mapController.animateTo(new GeoPoint(0,0));
	        centerMyPosition();
	        
	        
	        mapOverlays = mapView.getOverlays();
	    //    Drawable drawable = getResources().getDrawable(R.drawable.marker);
	        
	 //       OverlayItem overlayItem = new OverlayItem(new GeoPoint(0,0), "titulo", "descripcion");     
	  //      MapItemizedOverlay itemizedoverlay = new MapItemizedOverlay(drawable, mapView.getContext(), mapView);
	   //     itemizedoverlay.addOverlay(overlayItem);
	    //    mapOverlays.add(itemizedoverlay);	        
	        
	}
	
	private void centerMyPosition(){
        myLocationOverlay = new MyLocationOverlay(this, mapView);
        mapView.getOverlays().add(myLocationOverlay);
        myLocationOverlay.enableCompass();
        myLocationOverlay.enableMyLocation();
        myLocationOverlay.runOnFirstFix(new Runnable() {
            public void run() {
                mapController.animateTo(myLocationOverlay.getMyLocation());
            }
        });
}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ruta);
 
        map = (MapView)findViewById(R.id.mapview);
 
        map.setBuiltInZoomControls(true); 
        
        inicializeMap();
        
        LugaresInformacion proxy = new LugaresInformacion();
        ArrayList<Punto> puntoss = null;
		try {
			puntoss=proxy.getPuntosCamino(-56.100915, -56.100915, -56.100915,-56.100915,"");
		} catch (Exception e) { 
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        
        List<GeoPoint> list = new ArrayList<GeoPoint>();
        for (int i = 0; i < puntoss.size(); i++) {
        	double primero=puntoss.get(i).getLatOrig()*1000000;
        	double segundo=puntoss.get(i).getLngOrig()*1000000;
        	int latitudee=(int) primero;
        	int longitudee=(int) segundo;
        	list.add(new GeoPoint(latitudee,longitudee));
        	if(i==puntoss.size()-1)
        	{
        		double tercero=puntoss.get(i).getLatDes()*1000000;
        		double cuarto=puntoss.get(i).getLngDes()*1000000;
            	int latitudeeFin=(int) tercero;
            	int longitudeeFin=(int) cuarto;
            	list.add(new GeoPoint(latitudeeFin,longitudeeFin));
        	}
        	
        
        }
        for(int i = 0; i < list.size(); i++){
         if(i>0){
          mapOverlays.add(new LinePathOverlay(list.get(i-1), list.get(i)));
         }

        }
    }
 
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_ruta, menu);
        return true;
    }   
    
    
    
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
    
    
    
}

